package org.nowcoder.leetcode.slidingWindow;

/**
 * Title  : 643. Maximum Average Subarray I
 * Source : https://leetcode.cn/problems/maximum-average-subarray-i/
 * Author : XrazYang
 * Date   : 2023-07-25
 */

public class LeetCode_643 {
    public double findMaxAverage(int[] nums, int k) {
        //处理特殊情况
        if (nums.length == 1) {
            return nums[0];
        }
        //固定长度的滑动窗口
        int left = 0, temp = 0;
        int sum = Integer.MIN_VALUE;
        for (int right = 0; right < nums.length; right++) {
            if (right < k) {
                temp += nums[right];
                sum = temp;
            } else {
                temp = temp + nums[right] - nums[left];
                if (sum < temp) {
                    sum = temp;
                }
                left++;
            }
        }
        return (sum + 0.0) / k;
    }
}
